package com.aw.baselib.widgets

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import com.aw.baselib.R
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.scwang.smartrefresh.layout.internal.InternalAbstract

/**
 * @author : hzw
 */
class MyRefreshView(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :
    InternalAbstract(context, attrs, defStyleAttr) {
    private val mIvImgLoading: ImageView
    private val mIvImgArrowDown: ImageView
    private val mIvImgArrowUp: ImageView
    private val mTvRefreshTxt: TextView
    private val mAnimation: Animation

    //    private AnimationDrawable animationDrawable;
    init {
        inflate(context, R.layout.layout_refresh_header, this)
        mIvImgLoading = findViewById(R.id.iv_imgLoading)
        mIvImgArrowDown = findViewById(R.id.iv_imgArrowDown)
        mIvImgArrowUp = findViewById(R.id.iv_imgArrowUp)
        mTvRefreshTxt = findViewById(R.id.tv_refreshTxt)

        // 加载动画A
        mAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_animation)
        //        iv_img.setImageResource(R.drawable.refresh_anim);
//        animationDrawable = (AnimationDrawable) iv_img.getDrawable();
    }

    override fun onFinish(layout: RefreshLayout, success: Boolean): Int {
        if (success) {
            mIvImgLoading.clearAnimation()
            //            animationDrawable.stop();
            mTvRefreshTxt.text = "刷新完成"
        } else {
            mIvImgLoading.clearAnimation()
            //            animationDrawable.stop();
            mTvRefreshTxt.text = "刷新失败"
        }
        super.onFinish(layout, success)
        return 500 //延迟500毫秒之后再弹回
    }

    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        when (newState) {
            RefreshState.PullDownToRefresh -> showView(TAG_REFRESH_PULLDOWN)
            RefreshState.ReleaseToRefresh -> showView(TAG_RELEASE)
            RefreshState.Refreshing -> showView(TAG_LOADING)
            else -> {}
        }
    }

    private fun showView(tag: Int) {
        when (tag) {
            TAG_DEFAULT -> {
                showView(mIvImgArrowDown, true)
                showView(mIvImgArrowUp, false)
                showView(mIvImgLoading, false)
            }

            TAG_REFRESH_PULLDOWN -> {
                showView(mIvImgArrowDown, true)
                showView(mIvImgArrowUp, false)
                showView(mIvImgLoading, false)
                mTvRefreshTxt.text = "下拉刷新"
            }

            TAG_RELEASE -> {
                showView(mIvImgArrowDown, false)
                showView(mIvImgArrowUp, true)
                showView(mIvImgLoading, false)
                mTvRefreshTxt.text = "松开刷新"
            }

            TAG_LOADING -> {
                showView(mIvImgArrowDown, false)
                showView(mIvImgArrowUp, false)
                showView(mIvImgLoading, true)
                mTvRefreshTxt.text = "正在加载"
                mIvImgLoading.startAnimation(mAnimation)
            }
        }
    }

    private fun showView(view: View?, show: Boolean) {
        if (view == null) {
            return
        }
        view.visibility = if (show) VISIBLE else GONE
    }

    companion object {
        private const val TAG_DEFAULT = 0
        private const val TAG_REFRESH_PULLDOWN = 1
        private const val TAG_LOADING = 2
        private const val TAG_RELEASE = 3
    }
}